import axios from "axios"

export class APIConstants {
    static readonly HOST = 'https://www.codeboy.com/zhsqapi/'

    //用户模块相关接口

    //停车模块相关接口

    //社区模块相关接口

    //....

    //管理员模块相关接口
    // static readonly ADMIN_LOGIN = APIConstants.HOST + 'admin/login'
    static readonly ADMIN_LOGIN = APIConstants.HOST + 'user/login'
    static readonly ADMIN_INFO = APIConstants.HOST + 'admin/info'
    static readonly ADMIN_UPDATE_PWD = APIConstants.HOST + 'admin/update/pwd'
    static readonly ADMIN_HOUSEHOLD_LIST = APIConstants.HOST + 'admin/household/list'
    static readonly ADMIN_HOUSEHOLD_DETAILS = APIConstants.HOST + 'admin/household/details'
    static readonly ADMIN_HOUSEHOLD_DELETE = APIConstants.HOST + 'admin/household/delete'
    static readonly ADMIN_HOUSEHOLD_UPDATE = APIConstants.HOST + 'admin/household/udpate'
    static readonly ADMIN_HOUSEHOLD_ADD = APIConstants.HOST + 'admin/household/add'
    static readonly ADMIN_HOUSEHOLD_BATCH_DELETE = APIConstants.HOST + 'admin/household/batch/delete'
    static readonly ADMIN_HOUSEHOLD_BATCH_ADD = APIConstants.HOST + 'admin/household/batch/add'
    static readonly ADMIN_HOUSEHOLD_BATCH_EXPORT = APIConstants.HOST + 'admin/household/batch/export'
    static readonly ADMIN_HOUSEHOLD_FULL_EXPORT = APIConstants.HOST + 'admin/household/full/export'
}

/**
 * 发起GET请求
 * @param url 接口地址
 * @param params 查询参数
 * @param withToken 是否需要携带身份令牌
 */
export const getRequest = async (url:string, params={}, withToken=false)=>{
    let res = await axios({
        method: 'GET',
        url,
        params,
        headers: {
            token: withToken ? sessionStorage['loginToken'] : undefined
        }
    })
    return res.data
}

/**
 * 发起POST请求
 * @param url 接口地址
 * @param body 请求主体
 * @param withToken 是否需要携带身份令牌
 */
export const postRequest = async (url:string, body={}, withToken=false)=>{
    let res = await axios({
        method: 'POST',
        url,
        headers: {
            'Content-Type': 'application/json',
            'token': withToken ? sessionStorage['loginToken'] : undefined
        },
        data: body
    })
    return res.data
}